<?php


namespace app\api\controller;
header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");
header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding");

use think\Db;
include_once("../application/api/controller/CCPRestSDK.php");

/**
 * Class Send
 * @package app\api\controller
 */
class Send
{
    //主帐号
    private $accountSid= 'aaf98f8951d8d1c10151e1b3810a1b16';

    //主帐号Token
    private $accountToken= '2cd4840a245e45af891333a214560082';

    //应用Id
    private $appId='8a48b551523a5c1201523b570bd203b4';

    //请求地址，格式如下，不需要写https://
    private $serverIP='app.cloopen.com';

    //请求端口
    private $serverPort='8883';

    //REST版本号
    private $softVersion='2013-12-26';


    /**
     * dev.ziwudiche.com/api/Send/sendTemplateSMS
     * 发送模板短信
     * @param to 手机号码集合,用英文逗号分开
     * @param datas 内容数据 格式为数组 例如：array('Marry','Alon')，如不需替换请填 null
     * @param $tempId 模板Id
     */
    public function sendTemplateSMS($to="",$datas="",$tempId="")
    {
        // 初始化REST SDK
        global $accountSid,$accountToken,$appId,$serverIP,$serverPort,$softVersion;
        $accountSid = $this->accountSid;
        $accountToken=$this->accountToken;

        $appId=$this->appId;
        $serverIP = $this->serverIP;
        $serverPort = $this->serverPort;
        $softVersion = $this->softVersion;
        $rest = new \REST($serverIP,$serverPort,$softVersion);
        $rest->setAccount($accountSid,$accountToken);
        $rest->setAppId($appId);
        $to = input("phone");
        $datas = [
            mt_rand('100000','999999'),'15'
        ];
        $tempId = input("tempId","78452");
        // 发送模板短信
        $result = $rest->sendTemplateSMS($to,$datas,$tempId);
        if($result == NULL ) {
            echo "result error!";
            exit();
        }
        if($result->statusCode!=0) {
//            echo "error msg :" . $result->statusMsg . "<br>";
            echo json_encode(["error_code"=>$result->statusCode,"msg"=>$result->statusMsg],JSON_UNESCAPED_UNICODE);die();
            //TODO 添加错误处理逻辑
        }else{
            $code_data = [
                'tel' => $to,
                'code' =>$datas[0],
                'effective_time' =>900,
                'ctime' => time(),
            ];
            $result=Db::name('verification_code')->where('tel',$to)->find();
            if ($result) {
                Db::name('verification_code')->where('tel',$to)->update([
                    'code'	=>	$datas[0],
                    'ctime' => time()]);
            } else {
                $codes = Db::name('verification_code')->insert( $code_data);
            }
            echo json_encode(["code"=>200,"msg"=>'success']);die();
//            echo "Sendind TemplateSMS success!<br/>";
//            // 获取返回信息
//            $smsmessage = $result->TemplateSMS;
//            echo "dateCreated:".$smsmessage->dateCreated."<br/>";
//            echo "smsMessageSid:".$smsmessage->smsMessageSid."<br/>";
            //TODO 添加成功处理逻辑
        }
    }

}